<?php

class ProfilesController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('register'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Profiles;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Profiles']))
		{
			$model->attributes=$_POST['Profiles'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->user_id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Profiles']))
		{
			$model->attributes=$_POST['Profiles'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->user_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Profiles');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Profiles('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Profiles']))
			$model->attributes=$_GET['Profiles'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Profiles::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}


	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='profiles-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

    public function upload($file_name, $destPath, $label){
        Yii::import('application.extensions.upload.Upload');
        $Upload = new Upload((isset($file_name) ? $file_name : null));
        $Upload->jpeg_quality  = 100;
        $Upload->no_script     = false;
        $Upload->image_ratio   = false;
        $Upload->image_resize  = false;
        $Upload->file_max_size = 1024*1024;

        // some vars
        $newName  = md5(rand(1,100).date('YmdHis').rand(1,1000));

        if ($Upload->uploaded) {
            $Upload->file_new_name_body = $newName;
            $Upload->process($destPath);

            // if was processed
            if ($Upload->processed) {
                return $Upload->file_dst_name;
                // write image filename on table
            } else {
                throw new CHttpException(400,$Upload->error);
            }
        } else {
            throw new CHttpException(400,'请选择'.$label);
        }
    }

    public function ck_FORM($form){
        $session=$this->session;
        $error=array();

        foreach($form as $key=>$val){
            if($val['type']=='text' || $val['type']=='textarea'){
                $session[$key]=Yii::app()->request->getParam($key, isset($session[$key])?$session[$key]:'');
                if(empty($session[$key]) && $val['require']==true){
                    array_push($error,'请设置'.$val['label']);
                }
            }elseif($val['type']=='username'){
                $session[$key]=Yii::app()->request->getParam($key, isset($session[$key])?$session[$key]:'');
                if(empty($session[$key])) {
                    array_push($error,'请设置'.$val['label']);
                }
                $user_old=Profiles::model()->find('nickname=:nickname',array(':nickname'=>$session[$key]));
                if(!empty($user_old) || $session[$key]=='admin'){
                    array_push($error,$val['label'].'已经存在');
                    $session[$key]='';
                }

                $username_old=Users::model()->find('username=:nickname',array(':nickname'=>$session[$key]));
                if(!empty($username_old) || $session[$key]=='admin'){
                    array_push($error,$val['label'].'已经存在');
                    $session[$key]='';
                }


                if(intval($session[$key]).'fuck' == $session[$key].'fuck'){
                    array_push($error,$val['label'].'不能为纯数字');
                    $session[$key]='';
                }
                if(count(explode('spcf',$session[$key]))>1){
                    array_push($error,$val['label'].'不能包含spcf关键词');
                    $session[$key]='';
                }
                if(count(explode('@',$session[$key]))>1){
                    array_push($error,$val['label'].'不能包含"@"字符');
                    $session[$key]='';
                }


                $password=Yii::app()->request->getParam('password', '');
                $repassword=Yii::app()->request->getParam('repassword', '');
                if($password==''){
                    array_push($error,'请设置密码');
                }
                elseif($repassword==''){
                    array_push($error,'请设置重复密码');
                }
                elseif($password!=$repassword){
                    array_push($error,'密码不一致');
                }
                elseif(strlen($password)<6){
                    array_push($error,'密码长度太短');
                }
                elseif(strlen($password)>16){
                    array_push($error,'密码长度过长');
                }
                $session['password']=$password;
                $session['repassword']='';

            }elseif($val['type']=='file'){
                if($val['require']==true){
                    if(!empty($session[$key]) && empty($_FILES[$key])){
                        continue;
                    }
                    //有图片继续上传
                    if(empty($_FILES[$key]['name'])) {
                        array_push($error,'请上传'.$val['label']);
                    }else{
                        try {
                            $destPath = Yii::app()->getBasePath().'/../images/user/temp/';
                            $session[$key.'_img']=$this->upload($_FILES[$key], $destPath, $val['label']);
                            $session[$key]='/images/user/temp/'.$session[$key.'_img'];
                        } catch (Exception $e) {
                            array_push($error,$e->getMessage());
                        }
                    }
                }elseif($val['require']==false){
                    if(!empty($_FILES[$key]['name'])) {
                        try {
                            $destPath = Yii::app()->getBasePath().'/../images/user/temp/';
                            $session[$key.'_img']=$this->upload($_FILES[$key], $destPath, $val['label']);
                            $session[$key]='/images/user/temp/'.$session[$key.'_img'];
                        } catch (Exception $e) {
                            array_push($error,$e->getMessage());
                        }
                    }
                }
            }elseif($val['type']=='concate'){
                $session['year']=Yii::app()->request->getParam('year', isset($session['year'])?$session['year']:0);
                $session['month']=Yii::app()->request->getParam('month', isset($session['month'])?$session['month']:0);
                $session['day']=Yii::app()->request->getParam('day', isset($session['day'])?$session['day']:0);
                if($session['year']==0 || $session['month']==0 || $session['day']==0){
                    array_push($error,'请正确设置'.$val['label']);
                }else{
                    $session[$key]=$session['year'].$session['month'].$session['day'];
                }
            }elseif($val['type']=='email'){
                $session[$key]=Yii::app()->request->getParam($key, isset($session[$key])?$session[$key]:'');
                $validate=new CEmailValidator();
                if($validate->validateValue($session[$key])===false){
                    array_push($error,'请输入正确的邮箱地址');
                    $session[$key]='';
                }

                $user_old=Users::model()->find('email=:email',array(':email'=>$session[$key]));
                if(!empty($user_old)){
                    array_push($error,'此邮箱已经存在,请更换邮件邮箱');
                    $session[$key]='';
                }
            }elseif($val['type']=='radio'){
                $session[$key]=Yii::app()->request->getParam($key, isset($session[$key])?$session[$key]:0);
                if(!in_array($session[$key], $val['range'])){
                    array_push($error,'请选择'.$val['label']);
                }
            } elseif($val['type']=='select'){
                $session[$key]=Yii::app()->request->getParam($key, '');
                if(count($session[$key])>3){
                    array_push($error,'希望参加的专项活动,最多只能选择三项');
                }elseif(is_array($session[$key])){
                    $check_item=array(
                        '',
                        '佩特里网',
                        '信息保密',
                        '软件工程',
                        '开放系统',
                        '互联网',
                        '普适计算',
                        '数据库',
                        '协同计算',
                        '游戏动漫',
                        '体系结构',
                        '服务计算',
                        '容错计算',
                        '系统软件',
                        '多媒体技术',
                        '办公自动化',
                        '中文信息技术',
                        '高性能计算',
                        '传感器网络',
                        '信息存储技术',
                        '理论计算机科学',
                        '计算机应用计算机安全',
                        '计算机工程与工艺',
                        '计算机教育教学',
                        '金融信息处理',
                        '人工智能与模式识别',
                        '多值逻辑与模糊逻辑',
                        '工业控制计算机',
                        '抗恶劣环境计算机',
                        '计算机辅助设计与图形学',
                        '网络与数据通信',
                        '虚拟现实与可视化技术'
                    );
                    if(count($session[$key])>0){
                        $check_name=array();
                        foreach($session[$key] as $key_i => $val_i){
                             $check_name[]=$check_item[$key_i];
                        }
                        $session[$key]=implode(',',$check_name);
//                        pd($session[$key]);
                    }
                }
            }

            if(isset($val['open']) && $val['open']==true){
                $session[$key.'_open']= Yii::app()->request->getParam($key.'_open', 0);
            }else{
                $info[$key]['open_value']=0;
            }
        }

        return $error;
    }

    public function get_username($user_id=0){
        $criteria=new CDbCriteria;

        $criteria->select='username';
        $criteria->order='username DESC';

        if($this->session['type_pg']==1){
            $ck_id=(isset($user_id) && intval($user_id)>0) ? intval($user_id) : 10000;
            $criteria->condition='username>'.$ck_id;
            $last_user=Users::model()->find($criteria);

            if(empty($last_user->username)) {
                $username=10001;
            }else{
                $username=intval($last_user->username)+1;
            }
        }elseif($this->session['type_pg']==2){
            $ck_id=(isset($user_id) && intval($user_id)>0) ? intval($user_id) : 1000;
            $criteria->condition='username>'.$ck_id.' AND username<10000';
            $last_user=Users::model()->find($criteria);
            if(empty($last_user->username)) {
                $username=1001;
            }else{
                $username=intval($last_user->username)+1;
            }
        }
//        pd($username);

        $ck_user=Users::model()->find('username='.$username);
        if(isset($ck_user->username) && $ck_user->username==$username){
            $username++;
            $this->get_username($username);
        }

        return $username;
    }

    public function save_user(){
        $session=$this->session;

        $user=new User();
        $user->username=$this->get_username();
        $user->password=UserModule::encrypting($session['password']);
        $user->email=$session['email'];
        $user->activkey=UserModule::encrypting($session['password']);
        $user->createtime=time();
        $user->lastvisit=0;
        $user->superuser=0;
        $user->status=1;
        if($user->save()){
            return $user;
        }else{
            return false;
        }
    }

    public function mv_IMG($user_id, $key){
        $session=$this->session;
        if(!empty($session[$key]) && !empty($session[$key.'_img'])){
            $old_img=Yii::app()->getBasePath().'/..'.$session[$key];
            $new_path=Yii::app()->getBasePath().'/../images/user/'.$user_id.'/';
            $new_img=$new_path.$session[$key.'_img'];
            if(!is_dir($new_path))
                mkdir($new_path,0777,true);
            if(is_file($old_img)){
                rename($old_img, $new_img);
                $session[$key]='/images/user/'.$user_id.'/'.$session[$key.'_img'];
            }
        }
    }

    public function save_ps($user_id){
        $session=$this->session;

        $save=false;
        $ps=ProfilesStatus::model()->findAllByPk($user_id);
        if(empty($ps)){
            $save = true;
            $ps=new ProfilesStatus();
        }

        $my_key= array(
            'user_id' => 'User',
            'username' => 'Username',
            'nickname' => 'Nickname',
            'idcard' => 'Idcard',
            'sex' => 'Sex',
            'birthday' => 'Birthday',
            'place' => 'Place',
            'nation' => 'Nation',
            'marry' => 'Marry',
            'address' => 'Address',
            'contact' => 'Contact',
            'post' => 'Post',
            'phone' => 'Phone',
            'im' => 'Im',
            'politics' => 'Politics',
            'jobtitle' => 'Jobtitle',
            'job' => 'Job',
            'blog' => 'Blog',
            'pphoto' => 'Pphoto',
            'highest_education' => 'Highest Education',
            'graduation_date' => 'Graduation Date',
            'school' => 'School',
            'department' => 'Department',
            'specialities' => 'Specialities',
            'research_directions' => 'Research Directions',
            'work_experience' => 'Work Experience',
            'scientific_result' => 'Scientific Result',
            'achievement' => 'Achievement',
            'social_position' => 'Social Position',
            'n_second_school_major' => 'N Second School Major',
            'n_computer_skill' => 'N Computer Skill',
            'n_computer_achievement' => 'N Computer Achievement',
            'n_skill_mark' => 'N Skill Mark',
            'n_work' => 'N Work',
            'n_activity' => 'N Activity',
            's_admission_date' => 'S Admission Date',
            's_studentid' => 'S Studentid',
            's_school_position' => 'S School Position',
            'c_legal_person' => 'C Legal Person',
            'c_hr_manager' => 'C Hr Manager',
            'c_contacter_tel' => 'C Contacter Tel',
            'c_introduce_photo' => 'C Introduce Photo',
            'c_id_photo' => 'C Id Photo',
            'c_introduce' => 'C Introduce',
            'c_board_members' => 'C Board Members',
            'type_pg' => 'Type Pg',
            'type_p_vasn' => 'Type P Vasn',
            'type_g_pebg' => 'Type G Pebg',
            'open' => 'Open',
            'c_email' => 'C Email',
        );
        foreach($my_key as $key=>$val){
            $ps->{$key}=(isset($session[$key.'_open']) && $session[$key.'_open']==1) ? 1 : 0;
        }

        if($save==true){
            $ps->user_id=$user_id;
            $ps->save();
        }else{
            $ps->update();
        }
//        pr($ps);
    }

    public function save_p($user_id){
        $session=$this->session;
        $save=false;
        $p=Profile::model()->findAllByPk($user_id);
        if(empty($p)){
            $save=true;
            $p=new Profiles();
        }

        $this->mv_IMG($user_id, 'c_id_photo');
        $this->mv_IMG($user_id, 'c_introduce_photo');
        $this->mv_IMG($user_id, 'pphoto');

        $my_key= array(
            'user_id' => 'User',
            'nickname' => 'Nickname',
            'idcard' => 'Idcard',
            'sex' => 'Sex',
            'birthday' => 'Birthday',
            'place' => 'Place',
            'nation' => 'Nation',
            'marry' => 'Marry',
            'address' => 'Address',
            'contact' => 'Contact',
            'post' => 'Post',
            'phone' => 'Phone',
            'im' => 'Im',
            'politics' => 'Politics',
            'jobtitle' => 'Jobtitle',
            'job' => 'Job',
            'blog' => 'Blog',
            'pphoto' => 'Pphoto',
            'highest_education' => 'Highest Education',
            'graduation_date' => 'Graduation Date',
            'school' => 'School',
            'department' => 'Department',
            'specialities' => 'Specialities',
            'research_directions' => 'Research Directions',
            'work_experience' => 'Work Experience',
            'scientific_result' => 'Scientific Result',
            'achievement' => 'Achievement',
            'social_position' => 'Social Position',
            'n_second_school_major' => 'N Second School Major',
            'n_computer_skill' => 'N Computer Skill',
            'n_computer_achievement' => 'N Computer Achievement',
            'n_skill_mark' => 'N Skill Mark',
            'n_work' => 'N Work',
            'n_activity' => 'N Activity',
            's_admission_date' => 'S Admission Date',
            's_studentid' => 'S Studentid',
            's_school_position' => 'S School Position',
            'c_legal_person' => 'C Legal Person',
            'c_hr_manager' => 'C Hr Manager',
            'c_contacter_tel' => 'C Contacter Tel',
            'c_introduce_photo' => 'C Introduce Photo',
            'c_id_photo' => 'C Id Photo',
            'c_introduce' => 'C Introduce',
            'c_board_members' => 'C Board Members',
            'type_pg' => 'Type Pg',
            'type_p_vasn' => 'Type P Vasn',
            'type_g_pebg' => 'Type G Pebg',
            'open' => 'Open',
            'c_email' => 'C Email',
        );
        foreach($my_key as $key=>$val){
            $p->$key=isset($session[$key]) ? $session[$key] :'';
        }
        $p->user_id=$user_id;
        if($save){
            $p->save();
        }else{
            $p->update();
        }

        $user_info=new UsersInfo();
        $user_info->user_id=$user_id;
        $user_info->save();

//        pd($p);
    }

    public function post_PEBG_INFO(){
        $session=$this->session;

        if($user=$this->save_user()){
            $this->save_ps($user->id);
            $this->save_p($user->id);

            $session->clear();
            $session->destroy();
            $session->close();

            $this->render('//site/info',array(
                'msg'=>'注册成功,您的用户ID是: <span class="username">'.$user->username.'</span>,可以用此ID（或者邮箱地址或者用户名）和设置的密码登陆',
                'direct'=>$this->createUrl('/'),
            ));
            return;
        }
    }

    public function post_S_EDU(){
        $session=$this->session;
        if($user=$this->save_user()){
            $this->save_ps($user->id);
            $this->save_p($user->id);

            $session->clear();
            $session->destroy();
            $session->close();

            $this->render('//site/info',array(
                'msg'=>'注册成功,您的用户ID是: <span class="username">spcf'.$user->username.'</span>,可以用此ID（或者邮箱地址或者用户名）和设置的密码登陆',
                'direct'=>$this->createUrl('/'),
            ));
            return;
        }
    }

    public function post_N_WORK(){
        $session=$this->session;
        if($user=$this->save_user()){
            $this->save_ps($user->id);

            $this->save_p($user->id);

            $session->clear();
            $session->destroy();
            $session->close();

            $this->render('//site/info',array(
                'msg'=>'注册成功,您的用户ID是: <span class="username">spcf'.$user->username.'</span>,可以用此ID（或者邮箱地址或者用户名）和设置的密码登陆',
                'direct'=>$this->createUrl('/'),
            ));
            return;
        }
    }

    public function post_AV_JOB(){
        $session=$this->session;
        if($user=$this->save_user()){

            $this->save_ps($user->id);

            $this->save_p($user->id);

            $session->clear();
            $session->destroy();
            $session->close();

            $this->render('//site/info',array(
                'msg'=>'注册成功,您的用户ID是: <span class="username">spcf'.$user->username.'</span>,可以用此ID（或者邮箱地址或者用户名）和设置的密码登陆',
                'direct'=>$this->createUrl('/'),
            ));
            return;
        }
    }


    public function ck_PEBG_INFO(){
        $form=array(
            'nickname'=>array(
                'label'=>'法定单位名称', 'require'=>true, 'type'=>'username'
            ),
            'password'=>array(
                'label'=>'密码', 'require'=>true, 'type'=>'password'
            ),
            'repassword'=>array(
                'label'=>'重复秘密', 'require'=>true, 'type'=>'repassword'
            ),
            'idcard'=>array(
                'label'=>'组织机构代码', 'require'=>true, 'type'=>'text'
            ),
            'c_legal_person'=>array(
                'label'=>'法人代表', 'require'=>true, 'type'=>'text'
            ),
            'birthday'=>array(
                'label'=>'单位成立时间', 'require'=>true, 'type'=>'concate','open'=>true
            ),
            'contact'=>array(
                'label'=>'通信地址', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'post'=>array(
                'label'=>'邮政编码', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'email'=>array(
                'label'=>'电子邮箱', 'require'=>true, 'type'=>'email','open'=>true
            ),
            'phone'=>array(
                'label'=>'联系电话', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'blog'=>array(
                'label'=>'单位网址', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'c_hr_manager'=>array(
                'label'=>'行政(人事)负责人', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'c_contacter_tel'=>array(
                'label'=>'联络人及移动电话', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'im'=>array(
                'label'=>'联络QQ', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'c_email'=>array(
                'label'=>'单位对外电子邮箱', 'require'=>true, 'type'=>'text','open'=>true
            ),
            'c_id_photo'=>array(
                'label'=>'组织机构代码证件照', 'require'=>true, 'type'=>'file'
            ),
            'c_introduce_photo'=>array(
                'label'=>'组织机构形象照', 'require'=>false, 'type'=>'file'
            ),
            'c_introduce'=>array(
                'label'=>'单位简况及其他关键信息描述', 'require'=>true, 'type'=>'text'
            ),
        );

        return $this->ck_FORM($form);
    }


    public function ck_NS_BASE(){
        $form=array                         (
            'nickname' => array('label'=>'姓 名', 'require'=>true, 'type'=>'username','open'=>true),
            'password' => array('label'=>'密码', 'require'=>true, 'type'=>'password','open'=>true),
            'repassword' => array('label'=>'重复密码', 'require'=>true, 'type'=>'repassword','open'=>true),
            'idcard' =>   array('label'=>'身份证号码', 'require'=>true, 'type'=>'text','open'=>true),
            'sex' =>   array('label'=>'性 别', 'require'=>true, 'type'=>'radio','open'=>true,'range'=>array(1,2)),
            'birthday' => array('label'=>'出生日期', 'require'=>true, 'type'=>'concate','open'=>true),
            'place' => array('label'=>'籍 贯', 'require'=>true, 'type'=>'text','open'=>true),
            'nation' =>  array('label'=>'民 族', 'require'=>true, 'type'=>'text','open'=>true),
            'marry' =>array('label'=>'婚姻状况', 'require'=>true, 'type'=>'radio','open'=>true,'range'=>array(1,2,3)),
            'contact' =>  array('label'=>'通信地址', 'require'=>true, 'type'=>'text','open'=>true),
            'address' => array('label'=>'家庭地址', 'require'=>false, 'type'=>'text','open'=>true),
            'post' =>   array('label'=>'邮政编码', 'require'=>true, 'type'=>'text','open'=>true),
            'email' =>  array('label'=>'电子邮箱', 'require'=>true, 'type'=>'email','open'=>true),
            'phone' => array('label'=>'联系电话', 'require'=>true, 'type'=>'text','open'=>true),
            'im' =>     array('label'=>'Q Q', 'require'=>true, 'type'=>'text','open'=>true),
            'politics' =>array('label'=>'政治面貌', 'require'=>true, 'type'=>'radio','open'=>true,'range'=>array(1,2,3)),
            'pphoto' => array('label'=>'个人照片', 'require'=>true, 'type'=>'file','open'=>true),
        );

        return $this->ck_FORM($form);
    }

    public function ck_S_EDU(){
        $form=array(
            'school' => array('label'=>'就读院校', 'require'=>true, 'type'=>'text','open'=>true),
            'department' => array('label'=>'系别', 'require'=>true, 'type'=>'text','open'=>true),
            'specialities' => array('label'=>'所学专业', 'require'=>true, 'type'=>'text','open'=>true),
            's_admission_date' => array('label'=>'入学时间', 'require'=>true, 'type'=>'text','open'=>true),
            'graduation_date' => array('label'=>'毕业时间', 'require'=>true, 'type'=>'text','open'=>true),
            's_studentid' => array('label'=>'学生证号', 'require'=>true, 'type'=>'text','open'=>true),
            's_school_position' => array('label'=>'担任何种校内职务', 'require'=>false, 'type'=>'text','open'=>true),
            'social_position' => array('label'=>'担任何种校外兼职', 'require'=>false, 'type'=>'text','open'=>true),
            'achievement' => array('label'=>'已经获取（包括）院系级以上的奖项', 'require'=>false, 'type'=>'text','open'=>true),
            'n_computer_skill' => array('label'=>'加盟学会希望获得的计算机技能服务', 'require'=>false, 'type'=>'text','open'=>true),
        );

        return $this->ck_FORM($form);
    }

    public function ck_N_EDU(){
        $form=array(
            'highest_education' => array('label'=>'最高学历', 'require'=>true, 'type'=>'radio','open'=>true,'range'=>array(1,2,3,4,5)),
            'graduation_date' => array('label'=>'毕业时间', 'require'=>true, 'type'=>'text','open'=>true),
            'school' => array('label'=>'就读院校', 'require'=>true, 'type'=>'text','open'=>true),
            'department' => array('label'=>'系别', 'require'=>true, 'type'=>'text','open'=>true),
            'specialities' => array('label'=>'所学专业', 'require'=>true, 'type'=>'text','open'=>true),
            'n_second_school_major' => array('label'=>'就读的第二学历高校名称及专业', 'require'=>false, 'type'=>'text','open'=>true),
            'n_computer_skill' => array('label'=>'计算机相关技能描述', 'require'=>false, 'type'=>'text','open'=>true),
            'n_computer_achievement' => array('label'=>'在计算机技能或学术方面获取的重大奖项', 'require'=>false, 'type'=>'text','open'=>true),
            'n_skill_mark' => array('label'=>'技能标签', 'require'=>true, 'type'=>'text','open'=>true),
        );

        return $this->ck_FORM($form);
    }

    public function ck_N_WORK(){
        $form=array(
            'n_work' => array('label'=>'工作经历', 'require'=>true, 'type'=>'text','open'=>true),
            'n_activity' => array('label'=>'希望参加的专题活动', 'require'=>true, 'type'=>'select','open'=>true),
        );

        return $this->ck_FORM($form);
    }

    public function ck_AV_BASE(){
        $form=array(
            'nickname'=>array(
                'type'=>'username',
                'require'=>true,
                'label'=>'姓 名',
                'msg'=>'(一经填写，不可更改)',
                'key'=>'nickname',
                'class'=>'text120',
                'open'=>false,
            ) ,
            'password'=>array(
                'type'=>'password',
                'require'=>true,
                'label'=>'设置密码',
                'key'=>'password',
                'class'=>'text120',
                'open'=>false,
            ),
            'repassword'=>array(
                'type'=>'password',
                'require'=>true,
                'label'=>'重复密码',
                'key'=>'repassword',
                'class'=>'text120',
                'open'=>false,
            ),
            'idcard'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'身份证号码',
                'key'=>'idcard',
                'class'=>'text',
                'open'=>false,
            ),
            'sex'=>array(
                'type'=>'radio',
                'require'=>true,
                'label'=>'性别',
                'key'=>'sex',
                'class'=>'',
                'range'=>array(1,2),
                'open'=>true,
                'msg'=>'(一经填写，不可更改)',
            ),
            'birthday'=>array(
                'type'=>'concate',
                'require'=>true,
                'label'=>'出生日期',
                'key'=>'birthday',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'(一经填写，不可更改)',
            ),
            'place'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'籍 贯',
                'key'=>'place',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'(一经填写，不可更改)',
            ),
            'nation'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'民 族',
                'key'=>'nation',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'(一经填写，不可更改)',
            ),
            'marry'=>array(
                'type'=>'radio',
                'require'=>true,
                'label'=>'婚姻状况',
                'key'=>'marry',
                'class'=>'',
                'range'=>array(1,2,3),
                'open'=>true,
                'msg'=>'',
            ),
            'address'=>array(
                'type'=>'text',
                'require'=>false,
                'label'=>'家庭地址',
                'key'=>'address',
                'class'=>'text',
                'open'=>true,
                'msg'=>'',
            ),

            'contact'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'通信地址',
                'key'=>'contact',
                'class'=>'text',
                'open'=>true,
                'msg'=>'',
            ),
            'post'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'邮政编码',
                'key'=>'post',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'',
            ),
            'email'=>array(
                'type'=>'email',
                'require'=>true,
                'label'=>'电子邮箱',
                'key'=>'email',
                'class'=>'text',
                'open'=>true,
                'msg'=>'',
            ),
            'phone'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'联系电话',
                'key'=>'phone',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'',
            ),
            'im'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'Q Q',
                'key'=>'im',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'',
            ),

            'politics'=>array(
                'type'=>'radio',
                'require'=>true,
                'label'=>'政治面貌',
                'key'=>'politics',
                'class'=>'',
                'range'=>array(1,2,3),
                'open'=>true,
                'msg'=>'',
            ),

            'jobtitle'=>array(
                'type'=>'text',
                'require'=>false,
                'label'=>'职 称',
                'key'=>'jobtitle',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'',
            ),
            'job'=>array(
                'type'=>'text',
                'require'=>false,
                'label'=>'职 务',
                'key'=>'job',
                'class'=>'text120',
                'open'=>true,
                'msg'=>'',
            ),
            'blog'=>array(
                'type'=>'text',
                'require'=>false,
                'label'=>'博客地址',
                'key'=>'blog',
                'class'=>'text',
                'open'=>true,
                'msg'=>'',
            ),
            'pphoto'=>array(
                'type'=>'file',
                'require'=>true,
                'label'=>'个人照片',
                'key'=>'pphoto',
                'class'=>'text',
                'open'=>false,
                'msg'=>'(上传文件不超过1M)',
            ),
        );

        return $this->ck_FORM($form);
    }

    public function ck_AV_EDU(){
        $form=array(
            'highest_education'=>array(
                'type'=>'radio',
                'require'=>true,
                'label'=>'最高学历',
                'key'=>'highest_education',
                'class'=>'',
                'range'=>array(1,2,3,4,5),
                'open'=>true,
            ),
            'graduation_date'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'毕业时间',
                'key'=>'graduation_date',
                'class'=>'text120',
                'open'=>true,
            ),
            'school'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'就读院校',
                'key'=>'school',
                'class'=>'text',
                'open'=>true,
            ),
            'department'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'系别',
                'key'=>'department',
                'class'=>'text',
                'open'=>true,
            ),
            'specialities'=>array(
                'type'=>'text',
                'require'=>true,
                'label'=>'所学专业',
                'key'=>'specialities',
                'class'=>'text',
                'open'=>true,
            ),
            'research_directions'=>array(
                'type'=>'textarea',
                'require'=>false,
                'label'=>'主要学术研究方向',
                'key'=>'research_directions',
                'class'=>'texta576',
                'open'=>true,
            ),

        );

        return $this->ck_FORM($form);
    }

    public function ck_AV_WORK(){
        $form=array(
            'work_experience'=>array(
                'type'=>'textarea',
                'require'=>false,
                'label'=>'工作经历',
                'key'=>'work_experience',
                'class'=>'texta576',
                'open'=>true,
            ),
            'scientific_result'=>array(
                'type'=>'textarea',
                'require'=>false,
                'label'=>'科研成果',
                'key'=>'scientific_result',
                'class'=>'texta576',
                'open'=>true,
            ),

        );
        return $this->ck_FORM($form);
    }

    public function ck_AV_JOB(){
        $form=array(
            'achievement'=>array(
                'type'=>'textarea',
                'require'=>false,
                'label'=>'获取的成就,荣誉及奖励',
                'key'=>'achievement',
                'class'=>'texta576',
                'open'=>true,
            ),
            'social_position'=>array(
                'type'=>'textarea',
                'require'=>false,
                'label'=>'担任的社会职务',
                'key'=>'social_position',
                'class'=>'texta576',
                'open'=>true,
            )
        );
        return $this->ck_FORM($form);
    }


    public $session;
    public function actionRegister()
    {
        $this->session=new CHttpSession;
        $this->session->open();
//        $this->session->clear();
//        $this->session->close();
        $clear_error=false;
        $setup=Yii::app()->request->getParam('setup',1);
        if(isset($this->session['setup']) && isset($setup)){
             if($setup<$this->session['setup']){
                 $clear_error=true;
             }
            if(($this->session['setup']-intval($setup))>1){
                 $this->session['setup']=1;
            }
        }


        $this->session['setup']=Yii::app()->request->getParam('setup', isset($this->session['setup'])?$this->session['setup']:1);

        $this->session['type_pg']=Yii::app()->request->getParam('type_pg', isset($this->session['type_pg'])?$this->session['type_pg']:0);
        $this->session['type_p_vasn']=Yii::app()->request->getParam('type_p_vasn', isset($this->session['type_p_vasn'])?$this->session['type_p_vasn']:0);
        $this->session['type_g_pebg']=Yii::app()->request->getParam('type_g_pebg', isset($this->session['type_g_pebg'])?$this->session['type_g_pebg']:0);

        //进入错误的位置
        if($this->session['setup']==7 && ($this->session['type_pg']!=1 || !in_array($this->session['type_p_vasn'], array(1,2)))){
            $this->session['setup']=1;
        }
        if($this->session['setup']==6){
            if($this->session['type_pg']!=1 || !in_array($this->session['type_p_vasn'], array(1,2,3)))
                $this->session['setup']=1;
        }
        if($this->session['setup']==5){
            if($this->session['type_pg']!=1 || !in_array($this->session['type_p_vasn'], array(1,2,3,4)))
                $this->session['setup']=1;
        }
        if($this->session['setup']==4){
            if($this->session['type_pg']!=1  && $this->session['type_pg']!=2)
                $this->session['setup']=1;
        }

        if($this->session['setup']==2){
            if(!($this->session['type_pg']==1 || $this->session['type_pg']==2)){
                $this->session['error']='请选择会员类型';
                $this->session['setup']=1;
            }
        }elseif($this->session['setup']==3){
            $this->session['ty']=Yii::app()->request->getParam('ty', isset($this->session['ty'])?$this->session['ty']:0);
            if(empty($this->session['ty'])){
                $this->session['error']='请同意注册协议';
                $this->session['setup']=2;
            }

            if($this->session['type_pg']==1){
                if(!in_array($this->session['type_p_vasn'], array(1,2,3,4))){
                    $this->session['error']='请选择个人会员类型';
                    $this->session['setup']=2;
                }
            }elseif($this->session['type_pg']==2){
                if(!in_array($this->session['type_g_pebg'], array(1,2,3,4))){
                    $this->session['error']='请选择团体会员类型';
                    $this->session['setup']=2;
                }
            }
        }elseif($this->session['setup']==4){
            if($this->session['type_pg']==1){
                if($this->session['type_p_vasn']==1 || $this->session['type_p_vasn']==2){
                    $this->session['error']=$this->ck_AV_BASE();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=3;
                    }
                }elseif($this->session['type_p_vasn']==3 || $this->session['type_p_vasn']==4){
                    $this->session['error']=$this->ck_NS_BASE();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=3;
                    }
                }
            }elseif($this->session['type_pg']==2){
                if(in_array($this->session['type_g_pebg'], array(1,2,3,4))){
                    $this->session['error']=$this->ck_PEBG_INFO();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=3;
                    }else{
                        $this->post_PEBG_INFO();
                    }
                }
            }
        }elseif($this->session['setup']==5){
            if($this->session['type_pg']==1){
                if($this->session['type_p_vasn']==1 || $this->session['type_p_vasn']==2){
                    $this->session['error']=$this->ck_AV_EDU();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=4;
                    }
                }elseif($this->session['type_p_vasn']==3){
                    $this->session['error']=$this->ck_N_EDU();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=4;
                    }
                }elseif($this->session['type_p_vasn']==4){
                    $this->session['error']=$this->ck_S_EDU();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=4;
                    }else{
                        $this->post_S_EDU();
                    }
                }
            }
        }elseif($this->session['setup']==6){
            if($this->session['type_pg']==1){
                if($this->session['type_p_vasn']==1 || $this->session['type_p_vasn']==2){
                    $this->session['error']=$this->ck_AV_WORK();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=5;
                    }
                }elseif($this->session['type_p_vasn']==3){
                    $this->session['error']=$this->ck_N_WORK();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=5;
                    }else{
                        $this->post_N_WORK();
                    }
                }
            }
        }elseif($this->session['setup']==7){
            if($this->session['type_pg']==1){
                if($this->session['type_p_vasn']==1 || $this->session['type_p_vasn']==2){
                    $this->session['error']=$this->ck_AV_JOB();
                    if(!empty($this->session['error'])){
                        $this->session['setup']=6;
                    }else{
                        $this->post_AV_JOB();
                    }
                }
            }
        }

        if($clear_error==true){
            $this->session['error']='';
        }
        if(!empty($this->session['setup'])){
            $this->render('setup/'.$this->session['setup'],array(
                'session'=>$this->session,
            ));
        }
    }
}
